package com.hm.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hm.ruiji.common.R;
import com.hm.ruiji.entity.Category;
import com.hm.ruiji.entity.Dish;
import com.hm.ruiji.entity.Setmeal;
import com.hm.ruiji.service.CategoryService;
import com.hm.ruiji.service.DishService;
import com.hm.ruiji.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;
    @Resource
    private DishService dishService;
    @Resource
    private SetMealService setmealService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("新建分类category：{}",category.toString());
        if (check(category.getName()))
            return R.error("'"+category.getName()+"'已存在");
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        log.info("page = {},pageSize = {}" ,page,pageSize);

        //构造分页构造器
        Page<Category> pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        //添加排序条件，根据sort排序
        queryWrapper.orderByDesc(Category::getSort);

        //执行查询
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 修改信息
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody  Category category) {
        log.info("对象信息{}",category.toString());
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 根据id删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long ids) {
        log.info("删除对象信息{}",ids);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, ids);
        int count = dishService.count(wrapper);

        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getCategoryId, ids);
        int count1 = setmealService.count(wrapper1);

        log.info("count={"+count+"}，count1={"+count1+"}");

        if (count1 > 0 || count > 0){
            return R.error("该分类下有关联产品，不可删除");
        }

        categoryService.removeById(ids);
        return R.success("删除成功");
    }

    /**
     * 按条件查询
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        // 查询条件
        wrapper.eq(category.getType() !=null, Category::getType, category.getType());
        // 添加排序条件
        wrapper.orderByDesc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }


    /**
     * 查重
     * @param name
     * @return
     */
    private boolean check(String name) {
        List<Category> list = categoryService.list(new QueryWrapper<Category>().select("name"));
        for (Category category : list) {
            if (name.equals(category.getName())) return true;
        }
        return false;
    }
}
